﻿package cn.gbase.jiangsu.data.transfer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;
import org.nutz.service.IdEntityService;

import cn.gbase.jiangsu.data.transfer.bean.SystemUser;
import cn.gbase.jiangsu.data.transfer.service.SystemUserService;
import cn.gbase.jiangsu.data.transfer.util.DefaultPasswordEncoder;

@IocBean(args = { "refer:dao" })
@At("/systemUser")
@Filters(@By(type = CheckSession.class, args = { "user", "/" }))
public class SystemUserAction extends IdEntityService<SystemUser> {

	@Inject
	private SystemUserService systemUserService;

	public SystemUserAction() {
		super();
	}

	public SystemUserAction(Dao dao) {
		super(dao);
	}

	/**
	 * 用户登录.
	 * 
	 * @param user
	 *            用户数据
	 * @param request
	 * @param session
	 */
	@At("/login")
	@POST
	@Ok(">>:/main")
	@Fail("jsp:/index")
	@Filters
	public void login(@Param("..") SystemUser user, HttpServletRequest request,
			HttpSession session) {

		//回传数据
		request.setAttribute("formdata", user);
		
		// 取得用户
		SystemUser u = this.dao().fetch(
				SystemUser.class,Cnd.where("username", "=", user.getUsername()));

		// 用户不存在
		if (u == null) {
			request.setAttribute("msg", "用户名不存在");
			throw new RuntimeException();
		}

		// 密码错误
		String encodePass = new DefaultPasswordEncoder("MD5").encode(user
				.getPw());
		if (!u.getPw().equals(encodePass)) {
			request.setAttribute("msg", "密码错误！");
			throw new RuntimeException();
		}

		// 保存到session
		session.setAttribute("user", u);
	}
	

	@At("/logout")
	@Ok(">>:/")
	@Filters
	public void logout(HttpServletRequest request,
			HttpSession session) {
		session.removeAttribute("user");
	}

	/**
	 * 跳转到修改密码页面.
	 */
	@At("/updatePW")
	@Ok("jsp:/systemUser/updatePW")
	public void toUpdatePage() {
	}

	/**
	 * 修改密码.
	 * @param user 用户信息
	 * @param request
	 * @param session
	 */
	@At("/updatePW")
	@POST
	@Ok(">>:/systemUser/ok")
	@Fail("jsp:/systemUser/updatePW")
	public void add(@Param("..") SystemUser user, HttpServletRequest request,
			HttpSession session) {
		
		// 取得用户
		SystemUser loginUser=(SystemUser)session.getAttribute("user");

		//输入密码加密
		String encodePass = new DefaultPasswordEncoder("MD5").encode(user
				.getPw());
		
		//比较
		if(!encodePass.equals(loginUser.getPw())){
			request.setAttribute("msg", "密码输入错误");
			throw new RuntimeException();
		}

		systemUserService.updatePWS(user);
	}

	@At("/ok")
	@Ok("jsp:/systemUser/ok")
	public void toOkPage(HttpServletRequest request) {
	}
	
	/**
	 * 跳转到修改密码页面.
	 */
	@At("/main")
	@Ok("jsp:/main")
	public void toMainPage() {
	}
	
}
